package cn.com.woxitech.dev.controller.websocket;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageExceptionHandler;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;

import cn.com.woxitech.dev.common.RoomWebSocketSession;
import cn.com.woxitech.dev.common.StrUtils;
import cn.com.woxitech.dev.model.gen.WmRoom;
import cn.com.woxitech.dev.model.gen.WmRoomChathistory;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.service.RoomService;

@Controller
public class AppChatWebsocketHandler {
	
	@Autowired
	private RoomService roomService;
	@Autowired
	private Environment env;
	

	@CrossOrigin
    @MessageMapping("/private/ppt/key/{key}")
    @SendTo("/ws/private/ppt/key/{key}")
	public Object ppt_key(@DestinationVariable String key, HashMap<String, Object> body, SimpMessageHeaderAccessor  headerAccessor) throws Exception {
        
		String sessionid = headerAccessor.getSessionId();
		Map<String, Object> mp = new HashMap<String,Object>();
//		mp.put("sessionid", sessionid);
		mp.put("type", StrUtils.getMapStr("type",body));
		mp.put("headimgurl", StrUtils.getMapStr("headimgurl",body));
		mp.put("name", StrUtils.getMapStr("name",body));
		mp.put("userid", StrUtils.getMapStr("userid",body));
		
		mp.put("pptserver", env.getProperty("pptserver"));
		mp.put("callbackurl", env.getProperty("pptcallbackurl"));
        return mp;
    }
	
	
	@CrossOrigin
    @MessageMapping("/private/self/{userid}/timestamp/{t}")
    @SendTo("/ws/private/self/{userid}/timestamp/{t}")
	public Object self(@DestinationVariable String userid,@DestinationVariable String t, HashMap<String, Object> body, SimpMessageHeaderAccessor  headerAccessor) throws Exception {
        
		String sessionid = headerAccessor.getSessionId();
		Map<String, Object> mp = new HashMap<String,Object>();
		mp.put("sessionid", sessionid);
        return mp;
    }
	
	
	@CrossOrigin
    @MessageMapping("/room/{roomid}/user/{userid}")
    @SendTo("/ws/room/{roomid}")
    public Object room(@DestinationVariable String roomid, @DestinationVariable String userid, HashMap<String, Object> body, SimpMessageHeaderAccessor  headerAccessor) throws Exception {
        
		Map<String, Object> mp = new HashMap<String,Object>();
		String sessionid = headerAccessor.getSessionId();
		Map<String, Object> attributes = RoomWebSocketSession.getInstance().getAttributes(sessionid);
		if(attributes == null || attributes.get("userInfo")==null){
			return mp;
		}
		WmUser userInfo = (WmUser)attributes.get("userInfo");
		String useruuid = (String)attributes.get("useruuid");
		WmRoom roomInfo = (WmRoom)attributes.get("roomInfo");
		String roomuuid = (String)attributes.get("roomuuid");
		
		
		
		String type = StrUtils.getMapStr("type",body);
		if(userInfo == null){
			type = "ignore";
			mp.put("type", type);
			return mp;
		}
		if(type == null){
			type = "ignore";
			
		}else if(type.equals("login") || type.equals("logout")){
			
			int concurrent = this.roomService.countOnlineUser(roomuuid, true);
			mp.put("attendcount", this.roomService.addAttendCount(roomInfo));
			mp.put("concurrent", concurrent);
    	    //记录最高并发人数
    	    this.roomService.saveMaxConcurrentUser(roomuuid, concurrent);
    	    
		}else if(type .equals("txt")){
			mp.put("content", StrUtils.getMapStr("content",body));
        	mp.put("msgid", StrUtils.getMapStr("msgid",body));
			
		}else if(type.equals("audio")){
			
			mp.put("newsrc", StrUtils.getMapStr("newsrc",body));
			mp.put("sec", StrUtils.getMapStr("sec",body));
			mp.put("ppt", StrUtils.getMapStr("ppt",body));
			mp.put("msgid", StrUtils.getMapStr("msgid",body));

		}else if(type.equals("img")){
			mp.put("newsrc", StrUtils.getMapStr("newsrc",body));
			mp.put("msgid", StrUtils.getMapStr("msgid",body));
		}else if(type.equals("takeback")){
			
			this.roomService.removeHistoryItem(StrUtils.getMapStr("msgid",body));
			mp.put("msgid", StrUtils.getMapStr("msgid",body));
		}
		mp.put("type", type);
		mp.put("headimgurl", userInfo.getWxHeadimgurl());
		mp.put("name", userInfo.getName());
		mp.put("time", System.currentTimeMillis());
		mp.put("useruuid", useruuid);
		mp.put("unionid", userInfo.getWxUnionid());
		mp.put("roomuuid", roomuuid);
		mp.put("sessionid", sessionid);
		
		if(!type.equals("takeback") && !type.equals("ignore")){
			saveHistory(mp);	
		}
		
        return mp;
    }


	private void saveHistory(Map<String, Object> mp) {
		// TODO Auto-generated method stub
		WmRoomChathistory wc = new WmRoomChathistory();
		wc.setContent(StrUtils.getMapStr("content",mp));
		wc.setMsgid(StrUtils.getMapStr("msgid",mp));
		wc.setNewsrc(StrUtils.getMapStr("newsrc",mp));
		wc.setPpt(StrUtils.getMapStr("ppt",mp));
		wc.setRoomuuid(StrUtils.getMapStr("roomuuid",mp));
		wc.setSec(StrUtils.getMapStr("sec",mp));
		wc.setTime(System.currentTimeMillis());
		wc.setType(StrUtils.getMapStr("type",mp));
		wc.setUseruuid(StrUtils.getMapStr("useruuid",mp));
		
		this.roomService.saveChathistory(wc);
		
	}
	

}
